home *** CD-ROM | disk | FTP | other *** search
/ SunSoft Catalyst CDWARE 1996 May to August / Catalyst CDWARE 1996 May to August.iso / .products / FSA / classes / dance.java next >
Text File  |  1996-02-02  |  6KB  |  278 lines

  1. /*
  2.  * @(#)DancingText.java    1.2 95/06/08 James Gosling
  3.  * 
  4.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * Permission to use, copy, modify, and distribute this software and its
  7.  * documentation for NON-COMMERCIAL purposes and without fee is hereby
  8.  * granted provided that this copyright notice appears in all copies. Please
  9.  * refer to the file "copyright.html" for further important copyright and
  10.  * licensing information.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
  15.  * OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY
  16.  * LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR
  17.  * ITS DERIVATIVES.
  18.  */
  19.  
  20. import java.io.InputStream;
  21. import awt.*;
  22. import browser.*;
  23. import browser.audio.AudioData;
  24. import net.www.html.*;
  25.  
  26. /**
  27.  * A simple Item class to play an image loop.  The "img" tag parameter
  28.  * indicates what image loop to play.
  29.  *
  30.  * @author     James Gosling
  31.  * @version     1.16, 10 Dec 1994
  32.  */
  33. public
  34. class DancingText extends Applet implements Runnable {
  35.     char text[];
  36.  
  37.     /**
  38.      * The thread animating the images.
  39.      */
  40.     private Thread kicker = null;
  41.  
  42.     private int time;
  43.     private int delay;
  44.     private int pause;
  45.     private int style;
  46.     private String audioName;
  47.  
  48.     private int t0;
  49.     private boolean atStart = true;
  50.     private int x0[];
  51.     private int xd[];
  52.     private AudioData ad;
  53.     private boolean fetchingAudio;
  54.     private boolean playRequested;
  55.  
  56.     /**
  57.      * Initialize the applet. Get attributes.
  58.      */
  59.     public void init() {
  60.     try {
  61.         String s = null;
  62.         int W = width;
  63.         int H = height;
  64.         try {
  65.         s = getAttribute("text");
  66.         } catch(Exception e) {
  67.         }
  68.         if (s == null)
  69.         s = "Your message here";
  70.         text = new char[s.length() + 2];
  71.         s.getChars(0, s.length(), text, 0);
  72.         try {
  73.         time = Integer.parseInt(getAttribute("time"));
  74.         } catch(Exception e) {
  75.         time = 1000;
  76.         }
  77.         try {
  78.         audioName = getAttribute("sound");
  79.         } catch(Exception e) {
  80.         }
  81.         try {
  82.         delay = Integer.parseInt(getAttribute("delay"));
  83.         } catch(Exception e) {
  84.         delay = 50;
  85.         }
  86.         try {
  87.         pause = Integer.parseInt(getAttribute("pause"));
  88.         } catch(Exception e) {
  89.         pause = 0;
  90.         }
  91.         try {
  92.         style = Integer.parseInt(getAttribute("style"));
  93.         } catch(Exception e) {
  94.         style = 0;
  95.         }
  96.         String name = getAttribute("font");
  97.         if (name == null)
  98.         name = "TimesRoman36i";
  99.         int fstyle = 0;
  100.         int size = 0;
  101.         int pos = name.length();
  102.         int c = 0;
  103.         while (pos > 0) {
  104.         c = name.charAt(--pos);
  105.         switch (c) {
  106.           case 'b':
  107.             fstyle |= Font.BOLD;
  108.             continue;
  109.           case 'i':
  110.             fstyle |= Font.ITALIC;
  111.             continue;
  112.           case 'u':
  113.             fstyle |= Font.UNDERLINE;
  114.             continue;
  115.         }
  116.         break;
  117.         }
  118.         int fac = 1;
  119.         while ('0' <= c && c <= '9') {
  120.         size += (c - '0') * fac;
  121.         if (--pos <= 0)
  122.             break;
  123.         c = name.charAt(pos);
  124.         fac = fac * 10;
  125.         }
  126.         if (size <= 0)
  127.         size = 24;
  128.         name = name.substring(0, pos + 1);
  129.         Font f = getFont(name, fstyle, size);
  130.         if (f == null)
  131.         f = font;
  132.         else
  133.         font = f;
  134.         if (H < f.height)
  135.         H = f.height;
  136.         int len = text.length;
  137.         x0 = new int[len];
  138.         xd = new int[len];
  139.         int x = 0;
  140.         for (int i = 0; i < len; i++) {
  141.         xd[i] = x;
  142.         x += f.widths[text[i]];
  143.         }
  144.         if (x > W)
  145.         W = x;
  146.         if (W > x) {
  147.         int dx = (W - x) / 2;
  148.         for (int i = 0; i < len; i++)
  149.             xd[i] += dx;
  150.         }
  151.         resize(W, H);
  152.         switch (style) {
  153.           default:
  154.           case 1:
  155.         for (int i = 0; i < len; i++)
  156.             x0[i] = W / 2;
  157.         break;
  158.           case 2:
  159.         for (int i = 0; i < len; i++)
  160.             x0[i] = W;
  161.         break;
  162.           case 3:
  163.         for (int i = 0; i < len; i++)
  164.             x0[i] = 0;
  165.         break;
  166.           case 4:
  167.         for (int i = 0; i < len; i++)
  168.             x0[i] = xd[i] + W;
  169.         break;
  170.           case 5:
  171.         for (int i = 0; i < len; i++)
  172.             x0[i] = xd[len - i - 1];
  173.         break;
  174.           case 6:
  175.         for (int i = 0; i < len; i++)
  176.             x0[i] = (i & 1) == 0 ? W : 0;
  177.         break;
  178.           case 7:
  179.         for (int i = 0; i < len; i++)
  180.             x0[i] = (int) (W * Math.random());
  181.         break;
  182.         }
  183.         t0 = System.nowMillis();
  184.         atStart = true;
  185.     } catch(Exception e) {
  186.         e.printStackTrace();
  187.     }
  188.     }
  189.  
  190.     /**
  191.      * Run the image loop. This methods is called by class Thread.
  192.      * @see java.lang.Thread
  193.      */
  194.     public void run() {
  195.     try {
  196.         if (audioName != null && !fetchingAudio) {
  197.         fetchingAudio = true;
  198.         kicker = new Thread(this);
  199.         kicker.start();
  200.         Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
  201.         ad = getAudioData(audioName);
  202.         if (playRequested)
  203.             play(ad);
  204.         return;
  205.         }
  206.         if (pause > 0)
  207.         Thread.sleep(pause);
  208.         t0 = System.nowMillis();
  209.         atStart = false;
  210.         while (kicker != null) {
  211.         repaint();
  212.         Thread.sleep(delay);
  213.         }
  214.         kicker = null;
  215.         if (ad != null)
  216.         play(ad);
  217.         else
  218.         playRequested = true;
  219.     } catch(Exception e) {
  220.         e.printStackTrace();
  221.     }
  222.     }
  223.  
  224.     /**
  225.      * Paint the current frame.
  226.      */
  227.     public void paint(Graphics g) {
  228.     try {
  229.         float A;
  230.         if (atStart) {
  231.         atStart = false;
  232.             t0 = System.nowMillis();
  233.         A = 0.0;
  234.         } else {
  235.         A = 1.0;
  236.         if (kicker != null) {
  237.             int t = System.nowMillis() - t0;
  238.             if (t < time)
  239.             A = t / (float) time;
  240.             else
  241.             kicker = null;
  242.         }
  243.         }
  244.         int lim = text.length - 2;
  245.         int y = font.ascent;
  246.         for (int i = 0; i < lim; i++)
  247.         g.drawChars(text, i, 1,
  248.                 (int) (A * xd[i] + (1 - A) * x0[i]), y);
  249.     } catch(Exception e) {
  250.         System.out.print("paint\n");
  251.         e.printStackTrace();
  252.     }
  253.     }
  254.  
  255.     public void mouseDown(int x, int y) {
  256.     start();
  257.     }
  258.  
  259.     /**
  260.      * Start the applet by forking an animation thread.
  261.      */
  262.     public void start() {
  263.     atStart = true;
  264.     if (kicker == null) {
  265.         kicker = new Thread(this);
  266.         kicker.start();
  267.     }
  268.     }
  269.  
  270.     /**
  271.      * Stop the applet. The thread will exit because kicker is set to null.
  272.      */
  273.     public void stop() {
  274.     atStart = true;
  275.     kicker = null;
  276.     }
  277. }
  278.